package com.itqf.controller;


import com.itqf.entity.PageBean;
import com.itqf.entity.Product;
import com.itqf.service.ProductService;
import com.itqf.service.impl.ProductServiceImpl;
import com.itqf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@WebServlet(name = "ProductController", value = "/product")
public class ProductController extends BaseServlet {
    //初始化业务逻辑对象
    private static ProductService productService = new ProductServiceImpl();

    /**
     * 分页展示
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //1.接收请求参数 tid 类别id
        String tid = request.getParameter("tid");
        //从前端获取，当前页数和页容量
        int pageSize = 6;

        String currentPage = request.getParameter("currentPage");
        int page = 1;
        if (currentPage != null) {
            page = Integer.valueOf(currentPage);
        }
        //2.调用业务逻辑得到前端需要展示的PageBean
        PageBean<Product> pageBean = productService.findPage(tid,page,pageSize);

        //3.响应
        request.setAttribute("pageBean", pageBean);
        return Constants.FORWARD+"/goodsList.jsp";
    }

    /**
     * 点击商品名称跳转至商品介绍
     * @param request
     * @param response
     * @return
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取请求参数
        String pid = request.getParameter("pid");
        //2.调用业务逻辑
        Product product = productService.findProductByPid(pid);
        //3.响应
        //将查询到的商品信息存储于request的作用域中
        request.setAttribute("product",product);
        return Constants.FORWARD+"/goodsDetail.jsp" ;
    }
}
